/**
 * Created by GSY on 16/8/30.
 */
angular.module('starter.htjdController', [])
  .controller('HtjdCtrl', function ($scope, $loading, $state, $http) {

    $scope.docList = [];

    $scope.goMore = function (type) {
      $state.go('tab.htjd-more',{args:{type:type}});
    }
    $scope.showDocContent = function(url,title) {
      $state.go('tab.htjd-doc',{args:{url:url,title:title}});
    }

    function setData(data) {
      for(var i = 0; i < data.projects.length; i++) {
        data.projects[i].titleClass = data.projects[i].secClass.substring(data.projects[i].secClass.length-2,data.projects[i].secClass.length);
      }
    }

    setTimeout(function () {

      $http.get("http://www.ht-home.cn/rest/appservice/getArticles?pageNum=7&curPage=0&firstClass=policy", {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
      }).success(function (data, status, config, header) {
          setData(data);
          for(var i = 0; i<data.projects.length; i++) {
            $scope.docList.push(data.projects[i]);
          }
        })
        .error(function (data, status, config, header) {

        });

    }, 20);

  })

  .controller('HtjdDocCtrl', function ($sce,$sceDelegate,$scope, $stateParams, $loading, $state, $http) {
    $scope.vm = {title:$stateParams.args.title};
    $scope.docUrl = $stateParams.args.url;
    $scope.docUrl = $sce.trustAsResourceUrl($scope.docUrl);
  })

  .controller('HtjdMoreCtrl', function ($scope, $stateParams, $loading, $state, $http) {

    $scope.docList = [];
    $scope.vm = {'isLast':false,'isLoading':false,'currentPage':1,'showInfinite':true,noNet:false};
    var url = 'http://www.ht-home.cn/rest/appservice/getArticles?pageNum=20&curPage='+$scope.vm.currentPage + '&firstClass=' + $stateParams.args.type;

    function setData(data) {
      for(var i = 0; i < data.projects.length; i++) {
        data.projects[i].titleClass = data.projects[i].secClass.substring(data.projects[i].secClass.length-2,data.projects[i].secClass.length);
      }
    }

    $scope.showDocContent = function(url) {
      $state.go('tab.htjd-doc',{args:{url:url}});
    }

    //上拉获取新文章
    $scope.loadMoreDoc = function() {
      url = 'http://www.ht-home.cn/rest/appservice/getArticles?pageNum=20&curPage='+$scope.vm.currentPage + '&firstClass=' + $stateParams.args.type;
      $scope.vm.showInfinite = true;
      if(!$scope.vm.isLoading) {
        $scope.vm.isLoading = true;
        $http.get(url, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
          }
        }).success(function (data, status, config, header) {
            $scope.vm.showInfinite = false;
            $scope.vm.currentPage++;
            $scope.vm.isLoading = false;
            $scope.$broadcast('scroll.infiniteScrollComplete');
            setData(data);
            for(var i = 0; i<data.projects.length; i++) {
              $scope.docList.push(data.projects[i]);
            }
            if(data.projects && data.projects.length > 0 && data.projects.length == 20) {
              $scope.vm.showInfinite = true;
            }

            //$loading.showSuccess("登录成功");
          })
          .error(function (data, status, config, header) {
            $scope.vm.isLoading = true;
            $scope.vm.showInfinite = false;
            $scope.vm.noNet = true;
          });
      }
    }

  });
